import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { AdmBookStoreVO, AdmBookStoreForm, AdmBookStoreQuery } from '@/api/general/administration/book/store/types';

/**
 * 查询图书入库列表
 * @param query
 * @returns {*}
 */

export const listAdmBookStore = (query?: AdmBookStoreQuery): AxiosPromise<AdmBookStoreVO[]> => {
  return request({
    url: '/system/admBookStore/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询图书入库详细
 * @param id
 */
export const getAdmBookStore = (id: string | number): AxiosPromise<AdmBookStoreVO> => {
  return request({
    url: '/system/admBookStore/' + id,
    method: 'get'
  });
};

/**
 * 新增图书入库
 * @param data
 */
export const addAdmBookStore = (data: AdmBookStoreForm) => {
  return request({
    url: '/system/admBookStore',
    method: 'post',
    data: data
  });
};

/**
 * 修改图书入库
 * @param data
 */
export const updateAdmBookStore = (data: AdmBookStoreForm) => {
  return request({
    url: '/system/admBookStore',
    method: 'put',
    data: data
  });
};

/**
 * 删除图书入库
 * @param id
 */
export const delAdmBookStore = (id: string | number | Array<string | number>) => {
  return request({
    url: '/system/admBookStore/' + id,
    method: 'delete'
  });
};

/**
 * 导入图书 Excel
 * @param file 文件对象
 * @param bookStoreBo 额外参数对象
 */
export const importBooks = (file: File, bookStoreBo: AdmBookStoreForm): AxiosPromise<void> => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('data', JSON.stringify(bookStoreBo));

  return request({
    url: '/system/admBookStore/import',
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' }
  });
};

/**
 * 下载图书导入模板
 */
export const downloadTemplate = (): AxiosPromise<Blob> => {
  return request({
    url: '/system/admBookStore/downloadTemplate',
    method: 'get',
    responseType: 'blob'
  });
};

/**
 * 查询书籍
 * @param keyword 可选，书名/编号/拼音模糊搜索
 */
export const searchBooks = (keyword?: string): AxiosPromise<AdmBookStoreVO[]> => {
  return request({
    url: '/system/admBookStore/search',
    method: 'get',
    params: { keyword }
  });
};

/**
 * 批量添加图书
 * @param data 批量添加图书的请求参数
 * @returns
 */
export function batchAddBooks(data: any) {
  return request({
    url: '/system/admBookStore/batchAdd',
    method: 'post',
    data
  })
}

/**
 * 获取某个楼层最新编号
 * @param type 入库楼层
 */
export const getLatestNumber = (type: number): Promise<number> => {
  return request({
    url: `/system/admBookStore/latestNumber`,
    method: 'get',
    params: { type }
  }).then(res => res.data || 0)
}